window.onload = function () {
    TabBar('search');
    //提交按钮
    let kh_searchup = document.getElementById('kh_searchup')
    let kh_searchinput = document.getElementById("kh_searchinput")
    kh_searchup.addEventListener('click', () => {
        let searchVlaue = kh_searchinput.value
        let kh_searchhistory1 = document.getElementsByClassName("kh_searchhistory1")
        let kh_clear = document.getElementById("kh_clear")
        let kh_historysearch2 = document.getElementsByClassName("kh_historysearch2")
        let kh_delhistory=document.getElementsByClassName("kh_delhistory")
        for (let i = 0; i < kh_delhistory.length; i++) {
            kh_delhistory[i].addEventListener('click',()=>{
                console.log(i);
                kh_historysearch2[i].innerHTML=''     
            }
               )
            
        }
        kh_searchhistory1[0].innerHTML=''
        let kh_searchhistoryshow = document.getElementsByClassName("kh_searchhistoryshow")
        let kh_clearhistorysearch = document.getElementsByClassName("kh_clearhistorysearch")
        if(searchVlaue==''){
            kh_clear.style.display = "none"
        }
        if (searchVlaue != '') {
            kh_searchhistory1[0].style.display = 'block'

            kh_clear.style.display = "none"
            
                $create('kh_searchshow', 'kh_historysearch2',
                    `<div class='kh_searchhistoryshow'><span class='kh_value'>${searchVlaue}</span>
        <div class='kh_delhistory'>X</div>
        </div>
        `)
        let kh_searchhistoryshow=document.getElementsByClassName("kh_searchhistoryshow")
        let kh_value=document.getElementsByClassName("kh_value")
        for (let i = 0; i < kh_searchhistoryshow.length; i++) {
            kh_searchhistoryshow[i].addEventListener('click',function(){
                console.log(i);
                kh_value[i].innerHTML
                console.log(kh_value[i].innerHTML);  
                kh_searchinput.value=kh_value[i].innerHTML;
            
            })
            
        }
                for (let i = 0; i < kh_historysearch2.length; i++) {
                    kh_historysearch2[i].style.display = 'none'
                }
            }
            // console.log(kh_searchhistoryshow.length);
            if (kh_searchhistoryshow.length > 0) {
                kh_searchhistoryshow.style = 'block'
            }
            kh_clear.addEventListener('click', () => {
                for (let i = 0; i < kh_historysearch2.length; i++) {
                    kh_historysearch2[i].innerHTML = '';

                }

                kh_clear.style.display = 'none'
             
            
            })
        

        $ajax({
            url: `https://elm.cangdu.org/v4/restaurants?geohash=31.22967,121.4762&keyword=${searchVlaue}`,
            async: true,
            method: 'get',
            success: (res) => {
                console.log(res)
                let kh_history = document.getElementById("kh_history")
                let kh_canguandiv = document.getElementsByClassName("kh_canguandiv")
                let kh_searchhistory1 = document.getElementsByClassName("kh_searchhistory1")[0]
                if (res.length == 0) {

                    kh_history.innerHTML = `<center>很抱歉!没有无搜索结果</center>`
                    console.log(res.length);
                }

                if (searchVlaue == '') {
                    for (let i = 0; i < kh_historysearch2.length; i++) {
                        kh_historysearch2[i].style.display = 'block'
                        
                    }
                    if(kh_historysearch2.length>0){
                        kh_clear.style.display = 'block'
                    }

                    kh_clear.style.display = "block"
                    kh_history.innerHTML = `搜索历史`
                    kh_searchhistory1.innerHTML = ''
                    console.log(res.length);
                }
                if (res.length > 0) {
                    kh_history.innerHTML = `商家`

                    for (let i = 0; i < res.length; i++) {
                        $create('kh_searchhistory1', 'kh_canguandiv',
                            `<div class='kh_canguanimg'>
                    <img src='//elm.cangdu.org/img/${res[i].image_path}'></div>
                    <div class='kh_canguantext'>
                    <span class='kh_canguanname'>${res[i].name}</span>
                    <span class="kh_pay">支付</span><br>
                    <span class='kh_yueshou'>月售${res[i].recent_order_num}单</span><br>
                    <span class='kh_distance'>
                    ${res[i].float_minimum_order_amount}元起送/距离${res[i].distance}公里</span>
                    
                    </div>`
                        )
                        kh_canguandiv[i].addEventListener('click', () => {
                            window.location.href = `/pages/restaurant/restaurant.html?id=${res[i].id}&point=${res[i].rating}`
                        })

                    }
                }



            }
        })
    })

}